﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

[assembly: TagPrefix("ByteBlocks.FaceBookWeb", "FBLike")]


namespace ByteBlocks.FaceBookWeb
{
    #region Enumerations
    /// <summary>
    /// Type of layout.
    /// </summary>
    public enum LikeButtonLayout
    {
        Standard,
        ButtonCount,
        BoxCount
    }

    /// <summary>
    /// Verb to display with button
    /// </summary>
    public enum LikeButtonVerb
    {
        Like,
        Recommend
    }

    /// <summary>
    /// Color scheme for like button
    /// </summary>
    public enum LikeButtonColorScheme
    {
        Light,
        Dark
    }

    #endregion

    /// <summary>
    /// This class  impleemnts a control that helps in rendering HTML that
    /// displays FaceBook Like button on the page.
    /// </summary>
    public class FBLikeButton : Control, INamingContainer
    {
        #region Class Members
        const string FaceBookIFrame = "<iframe src=\"http://www.facebook.com/plugins/like.php?href={0}&amp;layout={1}&amp;show_faces={2}&amp;width={3}&amp;action={4}&amp;colorscheme={5}&amp;height={6}\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:{7}px; height:{8}px;\" allowTransparency=\"{9}\"></iframe>";
        #endregion

        /// <summary>
        /// Class constructors.
        /// </summary>
        /// <remarks>
        /// Sets up default values for all properties.
        /// </remarks>
        public FBLikeButton()
        {
            Layout = LikeButtonLayout.Standard;
            ColorScheme = LikeButtonColorScheme.Light;
            Width = 450;
            ShowFaces = true;
            Verb = LikeButtonVerb.Like;
        }
        #region Properties
        /// <summary>
        /// Gets or sets URL of page to associate with button
        /// </summary>
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets width of layout.
        /// </summary>
        public int Width
        {
            get;
            set;
        }

        /// <summary>
        /// Type of layout to use for the area.
        /// </summary>
        public LikeButtonLayout Layout
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets verb to display
        /// </summary>
        public LikeButtonVerb Verb
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets color scheme for Like button
        /// </summary>
        public LikeButtonColorScheme ColorScheme
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets option if profile picture to show
        /// next to comment or not.
        /// </summary>
        public bool ShowFaces
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets reference tag to include with the link.
        /// </summary>
        /// <remarks>
        /// The length of this tag can not be more than 50 characters.
        /// </remarks>
        public string RefTag
        {
            get;
            set;
        }
        #endregion

        /// <summary>
        /// Renders control.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            string html = string.Empty;
            try
            {
                HtmlGenericControl ul = GetFaceBookIFrame();
                if (null != ul)
                {
                    System.IO.StringWriter sw = new System.IO.StringWriter();
                    ul.RenderControl(new HtmlTextWriter(sw));
                    html = sw.ToString();
                }
            }
            catch (Exception ex)
            {
                //TODO: Log this message.
            }

            if (!string.IsNullOrEmpty(html))
            {
                writer.Write(html);
            }
        }

        #region Helper Methods
        void ValidateSettings()
        {
            if (Width <= 50)
            {
                Width = 450;
            }
        }

        HtmlGenericControl GetFaceBookIFrame()
        {
            ValidateSettings();

            HtmlGenericControl ctl = new HtmlGenericControl("div");
            LiteralControl lit = new LiteralControl();
            lit.Text = string.Format(FaceBookIFrame, 
                GetUrl(),
                GetLayout(),
                ShowFaces ? "true" : "false",
                Width,
                GetAction(),
                GetColorScheme(),
                60, // Height
                Width,
                60, // Height
                "true"); // Transparency
            ctl.Controls.Add(lit);
            return ctl;
        }

        string GetUrl()
        {
            return HttpUtility.UrlEncode(Url);
        }

        string GetLayout()
        {
            switch (Layout)
            {
                case LikeButtonLayout.ButtonCount:
                    return "button_count";
                case LikeButtonLayout.BoxCount:
                    return "box_count";
                default:
                    return "standard";
            }
        }

        string GetColorScheme()
        {
            switch (ColorScheme)
            {
                case LikeButtonColorScheme.Dark:
                    return "dark";
                default:
                    return "light";
            }
        }

        string GetAction()
        {
            switch (Verb)
            {
                case LikeButtonVerb.Recommend:
                    return "recommend";
                default:
                    return "like";
            }
        }
        #endregion
    }
}
